package com.jeff.cloud.consumermovie.controller;

import com.jeff.cloud.consumermovie.bean.User;
import com.jeff.cloud.consumermovie.feign.UserFeignClient;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.auth.BasicAuthRequestInterceptor;

import feign.codec.StringDecoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.FeignClientsConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author: jeff
 * @date: 2021/8/6 14:35
 */
//@Import导入的Fxxx是SpringCloud为Feign默认提供的配置类。
@Import(FeignClientsConfiguration.class)
@RestController
public class MovieController {
    private UserFeignClient userUserFeignClient;
    private UserFeignClient adminUserFeignClient;

    @Autowired
    public MovieController(Decoder decoder, Encoder encoder, Client client, Contract contract){
        //这边的decoder encoder client contract，可以Debug看看是什么实例
        this.userUserFeignClient = Feign.builder().client(client).encoder(encoder).client(client).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user","password1"))
                .target(UserFeignClient.class,"http://microservice-provider-user-with-auth/");
        this.adminUserFeignClient = Feign.builder().client(client).encoder(encoder).client(client).contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin","password2"))
                .target(UserFeignClient.class,"http://microservice-provider-user-with-auth/");
    }


    @GetMapping("/user-user/{id}")
    public String findByIdUser(@PathVariable Long id){
        return "你好user-user";
    }
    @GetMapping("/user-admin/{id}")
    public String findByIdAdmin(@PathVariable Long id){
        return this.adminUserFeignClient.findById(id);
    }
    /*@Autowired
    private RestTemplate restTemplate;
    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id){
        return this.restTemplate.getForObject("http://localhost:8000/"+id,User.class);
    }

    @Value("${user.userServiceUrl}")
    private String userServiceUrl;
    @GetMapping("/user2/{id}")
    public User findById2(@PathVariable Long id){
        return this.restTemplate.getForObject(this.userServiceUrl+id,User.class);
    }*/
}
